import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;

public class ThermostatTank {
    // 状态枚举（与状态图对应）
    public enum State { STANDBY, BOILING, SLEEP, MAINTENANCE }
    private State currentState;
    private int temperature; // 模拟温度
    private boolean hasWater; // 模拟有水状态
    private boolean isBroken; // 模拟是否烧坏

    public ThermostatTank() {
        currentState = State.STANDBY; // 启动后进入待机
        temperature = 25; // 初始温度
        hasWater = true;
        isBroken = false;
        startTimer(); // 启动时间监控
    }

    // 时间监控（23:00休眠，7:00唤醒）
    private void startTimer() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                if (hour == 23 && currentState != State.MAINTENANCE) {
                    currentState = State.SLEEP;
                    System.out.println("23:00 → 休眠状态，断开电源");
                } else if (hour == 7 && currentState == State.SLEEP) {
                    currentState = State.STANDBY;
                    System.out.println("7:00 → 唤醒，进入待机状态");
                }
            }
        }, 0, 60000); // 每分钟检查一次
    }

    // 状态更新逻辑（模拟传感器检测）
    public void checkAndUpdate() {
        if (isBroken) {
            currentState = State.MAINTENANCE;
            System.out.println("水箱烧坏 → 维修状态");
            return;
        }

        switch (currentState) {
            case STANDBY:
                if (temperature < 20) {
                    if (hasWater) {
                        currentState = State.BOILING;
                        System.out.println("温度<20°+有水 → 烧水状态");
                    } else {
                        System.out.println("温度<20°但无水 → 保持待机");
                    }
                }
                break;
            case BOILING:
                if (temperature >= 100) {
                    currentState = State.STANDBY;
                    System.out.println("温度≥100° → 停止烧水，待机状态");
                }
                break;
            case SLEEP:
                System.out.println("休眠中，不响应温度变化");
                break;
        }
    }

    // Setter方法（模拟传感器数据更新）
    public void setTemperature(int temp) { this.temperature = temp; }
    public void setHasWater(boolean hasWater) { this.hasWater = hasWater; }
    public void setBroken(boolean broken) { isBroken = broken; }

    // 测试方法
    public static void main(String[] args) throws InterruptedException {
        ThermostatTank tank = new ThermostatTank();
        // 场景1：温度降至15°（有水）
        tank.setTemperature(15);
        tank.checkAndUpdate();
        Thread.sleep(3000);
        // 场景2：温度升至100°
        tank.setTemperature(100);
        tank.checkAndUpdate();
        Thread.sleep(3000);
        // 场景3：水箱烧坏
        tank.setBroken(true);
        tank.checkAndUpdate();
    }
}